package vip.xumy.admin.sys.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vip.xumy.admin.sys.mapper.IActionLogMapper;
import vip.xumy.admin.sys.pojo.ActionLog;
import vip.xumy.admin.sys.pojo.Dictionary;
import vip.xumy.core.golbal.GlobalBuffer;
import vip.xumy.core.pojo.com.Cache;

/**	Ownership belongs to the company
 *	author:mengyxu
 *	date:2020年2月13日
 */

@Service
public class LogService {
	private static final String MAP_BUFFER_KEY = "log_type_mapping";
	@Autowired
	private IActionLogMapper actionLogMapper;
	@Autowired
	private DictionaryService dictService;

	public List<ActionLog> queryActionLog(ActionLog example) {
		return actionLogMapper.queryByExample(example);
	}

	public void insertActionLog(ActionLog log) {
		actionLogMapper.insert(log);
	}

	public void insertActionLogs(List<ActionLog> logs) {
		actionLogMapper.insertBatch(logs);
	}

	public Map<String, String> getTypeMap() {
		Cache cache = GlobalBuffer.getCache(MAP_BUFFER_KEY);
		if(Cache.isEmpty(cache)) {
			Map<String, String> map = new HashMap<>();
			for (Dictionary dict : dictService.getDict("log_type")) {
				map.put(dict.getCode(), dict.getName());
			}
			GlobalBuffer.addCache(MAP_BUFFER_KEY, new Cache(map,3600));
			return map;
		}else {
			return cache.getValue();
		}
	}

}
